import { createRouter, createWebHistory } from 'vue-router'
// 获取状态
import { useUserTokenStore } from '@hook/store'

import WhiteListRouter from './whiteListRouter'

// 初始化时只注册白名单，动态路由添加逻辑 请查看 hooks/store/module/menu.js
const routes = [
    ...WhiteListRouter,
]


const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

// 全局路由守卫,此处只校验token。 在 layout 做局部守卫校验权限
router.beforeEach((to, from, next) => {

    const { token } = useUserTokenStore()

    const isLoginRoute = to.path === '/login';

    document.title = to?.meta?.title || ''

    if(!token && !isLoginRoute){
        next('/login')
    }else{
        next()
    }
})


export default router